using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour {
	public static Player instance;
	[SerializeField]
	private GameObject candycrush;
	private GameManager manager;
	private GameObject click_candy_,target_candy_;
	private Move direction;
	void Start () {
		instance=this;
		DontDestroyOnLoad(this);
		manager=GameManager.getInstance();
	}
	void Update () {
		if(Application.loadedLevel==3){			
			switch(manager.gamestate)
			{ case GameState.playing:	manager.gamestate=GameState.moving;
					candycrush=GameObject.FindWithTag("candycrush");
					candycrush.GetComponent<CandyDemo>().Hint();
				Playing();	
				//Invoke("Playing",1f);
				break;
			case GameState.lose:	manager.gamestate=GameState.none;Invoke("Retry",3f);break;
			case GameState.win:manager.gamestate=GameState.none;Invoke("Win",3f); break;
			case GameState.nomorematch:
					manager.NoMove=false;
				manager.gamestate=GameState.none;
					Invoke("Suffle",4f);
				break;
			}
		}else if(Application.loadedLevel==1){
			 GameObject.FindWithTag("play").GetComponent<ButtonEvents>().Invoke("Play",1.5f);	
		}else if(Application.loadedLevel==2){
			GameObject.Find ("btn"+manager.lastLevel).GetComponent<MapButton>().Invoke("ButtonClick",1.5f);
			Invoke("ClickPlay",0.2f);
		}
	}
	private void Suffle(){
		manager.gamestate=GameState.nomorematch;
		CandyCheckMatch.instance.NoMoreMatch();
	}
	private void Win(){
		if(GameObject.FindWithTag("next_button")==null){return;}
		GameObject.FindWithTag("next_button").GetComponent<RetryButton>().NextLevel();
	}
	private void Retry(){
		GameObject.FindWithTag("retry_button").GetComponent<RetryButton>().RetryLevel();
	}
	private void ClickPlay(){
		if(GameObject.FindWithTag("play_level_button")!=null){
			GameObject.FindWithTag("play_level_button").GetComponent<LoadLevel>().Invoke("level",3f);
		}
	}

	private void Playing(){
		if(candycrush==null){return;}
		foreach(GameObject cs in candycrush.GetComponent<CandyDemo>().level.GetCandy()){
			cs.GetComponent<CandyController>().is_fall_down=false;
			cs.GetComponent<CandyController>().check=false;
		}

		if(!CheckToFindSpecial()){
			click_candy_=CandyDemo.instance.suggest_item1;	
			target_candy_=CandyDemo.instance.suggest_item2;
		}	
		Findirection(click_candy_,target_candy_);
		candycrush.GetComponent<CandyMovement>().SwapCandy(click_candy_);
		click_candy_=target_candy_=null;
	}
	private void Findirection(GameObject obj, GameObject other){
		if((obj==null)||(other==null)){return;}
		int i=0;
	A:
			manager.Direction=obj.GetComponent<CandyController>().GetDirectionNeighbor(other);
		i++;
		if((i>100)) {return;}
		if((manager.Direction==Move.none)){
			CandyCheckMatch.instance.CheckBoardState();
			goto A;
			
		}
	}
	private bool CheckToFindSpecial(){
		if(FindStar()){
			return true;
		}
		if(SwipeSpecialWithSpecial()){
			return true;
		}
		if(FindSpecial()){
			return true;
		}
		if(FindVerticalORHorizontal()){
			return true;
		}
		if(MoveSpecialToNearSpecial()){
			return true;
		}
		if(MatchContainSpecial()){
			return true;
		}
		return false;
	}
	Collider2D [] hit;
	private bool MoveSpecialToNearSpecial(){
		for(int i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
			if(CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().Type!="simple"){
				if(IsSpecialNear(CandyDemo.instance.level.GetCandy()[i])){
					if(MoveNeighborOfSpecial(CandyDemo.instance.level.GetCandy()[i])){
						return true;
					}
				}
			}
		}
		return false;
	}
	Vector3 other_special_pos;
	private bool MoveNeighborOfSpecial(GameObject obj){
		if(obj.transform.position.y>other_special_pos.y){
			for(int i=0;i<hit.Length;i++){
				if((hit[i].transform.position.y<obj.transform.position.y)&&(hit[i].tag=="Candy")){
					if(TestWithNeighbor(hit[i].gameObject)){
						return true;
					}
				}				                                                         
			}
		}
		if(obj.transform.position.y<other_special_pos.y){
			for(int i=0;i<hit.Length;i++){
				if((hit[i].transform.position.y>obj.transform.position.y)&&(hit[i].tag=="Candy")){
					if(TestWithNeighbor(hit[i].gameObject)){
						return true;
					}
				}				                                                         
			}
		}
		if(obj.transform.position.x<other_special_pos.x){
			for(int i=0;i<hit.Length;i++){
				if((hit[i].transform.position.x>obj.transform.position.x)&&(hit[i].tag=="Candy")){
					if(TestWithNeighbor(hit[i].gameObject)){
						return true;
					}
				}				                                                         
			}
		}
		if(obj.transform.position.x>other_special_pos.x){
			for(int i=0;i<hit.Length;i++){
				if((hit[i].transform.position.x<obj.transform.position.x)&&(hit[i].tag=="Candy")){
					if(TestWithNeighbor(hit[i].gameObject)){
						return true;
					}
				}				                                                         
			}
		}
		return false;
	}
	private bool IsSpecialNear(GameObject obj){
		hit=Physics2D.OverlapCircleAll(obj.transform.position,3f);
		for(int i=0;i<hit.Length;i++){
			if(hit[i].tag=="Candy"){
				if(hit[i].GetComponent<CandyController>().Type!="simple"){
					other_special_pos=hit[i].gameObject.transform.position;
					return true;
				}
			}
		}
		return false;
	}
	private bool TestWithNeighbor(GameObject obj){
		for(int i=0;i<CandyCheckMatch.instance.direction.Length;i++){
			if(CandyCheckMatch.instance.TestWithNeighbor(obj,CandyCheckMatch.instance.direction[i])){
				click_candy_=obj;
				target_candy_=obj.GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[i]);
				return true;
			}
		}
		return false;
	}
	private int i,j;
	private bool FindStar(){
		for(i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
			for(j=0;j<CandyCheckMatch.instance.direction.Length;j++){
				if(	CandyCheckMatch.instance.TestWithNeighbor(CandyDemo.instance.level.GetCandy()[i],CandyCheckMatch.instance.direction[j])){
					if((CandyCheckMatch.instance.v_list.Count>=5)||(CandyCheckMatch.instance.h_list.Count>=5)){
						click_candy_=CandyDemo.instance.level.GetCandy()[i];
						target_candy_=CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j]);
						return true;
					}
				}
			}
		}
		return false;
	}
	private bool FindSpecial(){
		for(i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
			for(j=0;j<CandyCheckMatch.instance.direction.Length;j++){
				if(	CandyCheckMatch.instance.TestWithNeighbor(CandyDemo.instance.level.GetCandy()[i],CandyCheckMatch.instance.direction[j])){
					if((CandyCheckMatch.instance.v_list.Count>=3)&&(CandyCheckMatch.instance.h_list.Count>=3)){
						click_candy_=CandyDemo.instance.level.GetCandy()[i];
						target_candy_=CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j]);
						return true;
					}
				}
			}
		}
		return false;
	}
	private bool FindVerticalORHorizontal(){
		for(i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
			for(j=0;j<CandyCheckMatch.instance.direction.Length;j++){
				if(	CandyCheckMatch.instance.TestWithNeighbor(CandyDemo.instance.level.GetCandy()[i],CandyCheckMatch.instance.direction[j])){
					if((CandyCheckMatch.instance.v_list.Count>3)||(CandyCheckMatch.instance.h_list.Count>3)){
						click_candy_=CandyDemo.instance.level.GetCandy()[i];
						target_candy_=CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j]);
						return true;
					}
				}
			}
		}
		return false;
	}
	private  bool SwipeSpecialWithSpecial(){
		for(i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
			for(j=0;j<CandyCheckMatch.instance.direction.Length;j++){
				if((CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().Type!="simple")&&
				   (CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j])!=null)){
					if(CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j]).GetComponent<CandyController>().Type!="simple"){
						click_candy_=CandyDemo.instance.level.GetCandy()[i];
						target_candy_=CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j]);
						return true;
					}
				}
			}
		}
		return false;
	}
		private bool MatchContainSpecial(){
			for(i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
				for(j=0;j<CandyCheckMatch.instance.direction.Length;j++){
					if(CandyCheckMatch.instance.TestWithNeighbor(CandyDemo.instance.level.GetCandy()[i],CandyCheckMatch.instance.direction[j])){
						if((IsContainSpecial(CandyCheckMatch.instance.v_list))||
					   (IsContainSpecial(CandyCheckMatch.instance.h_list))){
						click_candy_=CandyDemo.instance.level.GetCandy()[i];
						target_candy_=CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().GetNeighbor(CandyCheckMatch.instance.direction[j]);
						return true;
					}
				}
			}
		}
		return false;
	}
	private bool IsContainSpecial(List<GameObject> list){
		foreach(GameObject cs in list){
			if(cs.GetComponent<CandyController>().Type!="simple"){
				return true;
			}
		}
		return false;
	}
}
